# include <iostream>
# include <vector>
# include <string>
using namespace std;


int maxDiff(int num) {
    string pre = to_string(num);
    char target;
    for (int i = 0; i < pre.size(); ++i) {
        if (pre[i] == '9') continue;
        else {
            target = pre[i];
            for (int j = i; j < pre.size(); ++j) {
                if (pre[j] == target)
                    pre[j] = '9';
            }
            break;
        }
    }
    string next = to_string(num);
    if (next[0] != '1') {
        target = next[0];
        for (int i = 0; i < next.size(); ++i)
            if (next[i] == target) next[i] = '1';
    }
    else {
        int i = 1;
        for (; i < next.size(); ++i) {
            if (next[i] != '1' && next[i] != '0') {
                target = next[i];
                next[i] = '0';
                break;
            }
        }
        for (; i < next.size(); ++i) {
            if (next[i] == target)
                next[i] = '0';
        }

    }
    cout << pre << endl << next << endl;
    return stoi(pre) - stoi(next);
}

int main() {
    cout << maxDiff(12345678);
}